package cn.hesion.mergefile.step2;
/**
 * ClassName: CommenRecordWriter <br/>
 * Description: <br/>
 * date: 2021/2/4 11:05<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @program: ClientDemo
 * @description:
 * @author: hesion
 * @create: 2021-02-04 11:05
 **/
public class CommenRecordWriter extends RecordWriter<CommentBean, NullWritable> {
    FSDataOutputStream goodOut;
    FSDataOutputStream commomOut;
    FSDataOutputStream badOut;

    public CommenRecordWriter(FSDataOutputStream goodOut, FSDataOutputStream commomOut, FSDataOutputStream badOut) {
        this.goodOut = goodOut;
        this.commomOut = commomOut;
        this.badOut = badOut;
    }

    public CommenRecordWriter() {
    }

    @Override
    public void write(CommentBean key, NullWritable nullWritable) throws IOException, InterruptedException {
        int commentStatus = key.getCommentStatus();
        String beanstr = key.toString();
        if(commentStatus==0){
            goodOut.write(beanstr.getBytes());
            goodOut.write("\r\n".getBytes());
            goodOut.flush();
        }else if (commentStatus==1){
            commomOut.write(beanstr.getBytes());
            commomOut.write("\r\n".getBytes());
            commomOut.flush();
        }else {
            badOut.write(beanstr.getBytes());
            badOut.write("\r\n".getBytes());
            badOut.flush();
        }
    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        IOUtils.closeStream(goodOut);
        IOUtils.closeStream(commomOut);
        IOUtils.closeStream(badOut);
    }
}
